package com.example.yrdev.ordering_system.Report;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.yrdev.ordering_system.Adapter.ReportMemberAdapter;
import com.example.yrdev.ordering_system.Common.CommonActivity;
import com.example.yrdev.ordering_system.Database.DingHanDB;
import com.example.yrdev.ordering_system.Dialog.DatePickerDialog;

import com.example.yrdev.ordering_system.Model.MemberCardReportType;
import com.example.yrdev.ordering_system.R;
import com.example.yrdev.ordering_system.Utils.GlobalFunction;
import com.example.yrdev.ordering_system.handler.ReportMemberActivityHandler;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by yrdev on 8/18/16.
 */
//会员明细

public class ReportMemberActivity extends CommonActivity {
    public int reportType=7;

    ArrayList<HashMap>hashMapArrayList;

    @InjectView(R.id.report_list)ListView report_list;
    @InjectView(R.id.report_container)HorizontalScrollView report_container;
    @InjectView(R.id.reportType)TextView reportTypeTxt;

    @InjectView(R.id.start_date)Button startDateBut;
    @InjectView(R.id.end_date)Button endDateBut;

    String startDateVal= GlobalFunction.convertDate(new Date(),"yyyy/MM/dd 00:00:00");
    String endDateVal= GlobalFunction.convertDate(new Date(),"yyyy/MM/dd 23:59:00");

    Context context;
    ReportMemberAdapter customAdapter;
    ReportMemberActivityHandler reportMemberActivityHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showBackButton();
        setBarTitle("会员明细");
        hashMapArrayList=new ArrayList<>();
        reportMemberActivityHandler=new ReportMemberActivityHandler(this);

        //initView();
       initView();



       // yyyy/MM/dd HH:mm:ss 日期格式

     }

      ArrayList cardRecordArrayList;


    public void chooseDate(final View view){

        DatePickerDialog datePickerDialog=new DatePickerDialog(this);
        datePickerDialog.setDateListener(new DatePickerDialog.DateListener() {
            @Override
            public void getDate(String startDate, Date date) {

                switch (view.getId()){

                    case R.id.start_date:
                    startDateVal= GlobalFunction.convertDate(date,"yyyy/MM/dd 00:00:00");
                    startDateBut.setText(startDate);
                        break;

                    case R.id.end_date:
                       endDateVal= GlobalFunction.convertDate(date,"yyyy/MM/dd 23:59:00");
                        endDateBut.setText(startDate);
                        break;

                }
                reloadData();
            }

        });

        datePickerDialog.show();
    }


    @Override
    public void initView() {

        setContentView(R.layout.report_member_detail_layout);

        ButterKnife.inject(this);


        super.initView();

        startDateBut.setText(GlobalFunction.convertDate(new Date(),"yyyy/MM/dd"));
        endDateBut.setText(GlobalFunction.convertDate(new Date(),"yyyy/MM/dd"));



        context=ReportMemberActivity.this;

        changeReportType(null);



        reloadData();


        showRightButtonWithListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                uploadPurchaseRecord();


            }
        },"上传");


    }

    public void changeReportType(View view){
        ReportTypeDialog dialog=new ReportTypeDialog(this);
        dialog.show();

        dialog.setReportTypeListener(new ReportTypeDialog.ReportTypeListener() {
            @Override
            public void selectedReport(MemberCardReportType memberCardReportType) {

                reportType=Integer.valueOf(memberCardReportType.getReportId());
                reportTypeTxt.setText(memberCardReportType.getReportName());
                reloadData();


            }
        });

    }


    public void reloadData(){

        cardRecordArrayList= DingHanDB.getInstance(context).getCardReport(reportType,startDateVal,endDateVal);


       initAdapter();

    }


    private  void uploadPurchaseRecord(){


        if (cardRecordArrayList.size()>0)
        {
            reportMemberActivityHandler.setReportMemberListener(cardRecordArrayList, new ReportMemberActivityHandler.ReportMemberListener() {
                @Override
                public void uploadDone() {

                    cardRecordArrayList=   DingHanDB.getInstance(context).getCardReport("",reportType);
                    customAdapter.refresh(cardRecordArrayList);

                }
            });
        }
        else
            GlobalFunction.ToastAlert(this,"无记录可上传");


    }






    public void initAdapter(){

        if (cardRecordArrayList.size()==0){

            report_container.setVisibility(View.GONE);


        }else {

            report_container.setVisibility(View.VISIBLE);

            if (customAdapter==null)
            {
                customAdapter   = new ReportMemberAdapter(context,cardRecordArrayList);
                report_list.setAdapter(customAdapter);


            }
            else {

                customAdapter.refresh(cardRecordArrayList);

            }


        }

    }




}



